package com.aaa.jur.controller;


import com.aaa.common.entity.LayUiTable;
import com.aaa.common.entity.RoleMenu;
import com.aaa.jur.service.RoleMenuService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色和菜单关联表 前端控制器
 * </p>
 *
 * @author AAATeacherChen
 * @since 2020-06-14
 */
@Controller
@RequestMapping("/roleMenu")
public class RoleMenuController {
    @Autowired
    private RoleMenuService roleMenuService;
    @RequestMapping("/findMenuById")
    @ResponseBody
    public List findMenuById(Integer id){
        List result = new ArrayList();

        Wrapper<RoleMenu> wrapper = new EntityWrapper();
        wrapper.eq("role_id",id);

        List<RoleMenu> roleMenus = roleMenuService.selectList(wrapper);
        for (RoleMenu roleMenu : roleMenus) {
            result.add(roleMenu.getMenuId());
        }

        return result;
    }
    @RequestMapping("/updateRoleMenu")
    @ResponseBody
    public LayUiTable updateRoleMenu(@RequestBody Map arrayList){
        List list = (List) arrayList.get("list");
        Integer rId = (Integer) arrayList.get("rId");

        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("role_id",rId);
        boolean b = roleMenuService.delete(wrapper);
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(rId);
        if(b){
            for (int i = 0; i < list.size(); i++) {
                Map map = (Map) list.get(i);
                //System.out.println((Integer) map.get("menuId"));
                roleMenu.setMenuId((Integer) map.get("menuId"));
                b = roleMenuService.insert(roleMenu);
            }
        }
        LayUiTable layUiTable = new LayUiTable();
        layUiTable.setCode(0);
        layUiTable.setMsg("成功");
        layUiTable.setCount(0);
        layUiTable.setData(list);
        if(b){
            layUiTable.setMsg("成功");
        }else {
            layUiTable.setMsg("失败");
        }
        return layUiTable;
    }
}

